package com.innoking.docsys.workflow.service.impl;

import com.innoking.docsys.utils.ResultObjVO;
import com.innoking.docsys.utils.ResultUtil;
import com.innoking.docsys.workflow.componet.ActivitiAppointTask;
import com.innoking.docsys.workflow.dto.UpmsActDefine;
import com.innoking.docsys.workflow.module.WorkflowRunVo;
import com.innoking.docsys.workflow.service.WorkflowDefineService;
import com.innoking.docsys.workflow.service.WorkflowRunService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 描述:
 *
 * @author zyp
 * @description 执行工作流service实现类
 * @create 2019-09-09 17:20
 */
@Service
public class WorkflowRunServiceImpl implements WorkflowRunService {

    Logger logger= LoggerFactory.getLogger(WorkflowModelServiceImpl.class);

    @Autowired
    protected RuntimeService runtimeService;

    @Autowired
    protected TaskService taskService;

    @Autowired
    protected HistoryService historyService;

    @Autowired
    protected RepositoryService repositoryService;

    @Autowired
    protected WorkflowDefineService workflowDefineService;

    @Autowired
    private ActivitiAppointTask activitiAppointTask;
    /**
     * @author:zyp
     * @description 根据执行流程id下载资源
     * @date: 2019/9/9 17:37
     * @return 
     */
    @Override
    public void loadByProcessInstance(WorkflowRunVo workflowRunVo,HttpServletResponse response) {
        try {
            InputStream resourceAsStream = null;
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(workflowRunVo.getProcessInstanceId()).singleResult();
            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId())
                    .singleResult();

            String resourceName = "";
            if (workflowRunVo.getResourceType().equals("image")) {
                resourceName = processDefinition.getDiagramResourceName();
            } else if (workflowRunVo.getResourceType().equals("xml")) {
                resourceName = processDefinition.getResourceName();
            }
            resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = resourceAsStream.read(b, 0, 1024)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author:zyp
     * @description 设置第一个节点审批人的具体内容情况
     * @date: 2019/9/10 10:34
     * @return 
     */
    @Override
    public ResultObjVO selectFirstTaskUser(WorkflowRunVo workflowRunVo) {
        String activitiType = workflowRunVo.getActivitiType();
        String trem = workflowRunVo.getTrem();
        //获取当前借阅的最新的流程
        UpmsActDefine upmsActDefine = workflowDefineService.getNewAct(activitiType);
        if(upmsActDefine==null){
            return ResultUtil.error("检索失败，请找系统管理员确认流程是否定义");
        }
        //获取流程的唯一标识
        String key = upmsActDefine.getAdKey();
        Object result = activitiAppointTask.getFirstTaks(key,trem);
        return ResultUtil.success(result);
    }
}